package com.wn.airplane_mode.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.wn.airplane_mode.R;
import com.wn.airplane_mode.tool.AirplaneTaskObj;
import com.wn.airplane_mode.tool.AirplaneTool;
/**
 * @author wuning
 * */
public class AirplaneModeCycleSettings extends Activity {

	private AirplaneTool airplaneTool = null;
	private List<AirplaneTaskObj> taskList = new ArrayList<AirplaneTaskObj>();
	private LayoutInflater layoutInflater = null;
	private String[] week = null;
	private TaskAdapter taskAdapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting2);
		airplaneTool = AirplaneTool.getInstance(this);
		setDatas();
		ListView list = (ListView)findViewById(R.id.listView_tasklist);
		taskAdapter =new TaskAdapter(this); 
		list.setAdapter(taskAdapter);
	}
	
	
	private void setDatas(){
		week = getResources().getStringArray(R.array.weekday_list);
		for (int i = 1;i<=7;i++) {					
			taskList.add(new AirplaneTaskObj(airplaneTool.getTaskObjStr(i)));
		}
		
	}
	
	
	class TaskAdapter extends BaseAdapter{
		
		public TaskAdapter(Context context){
			layoutInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return taskList.size();
		}

		@Override
		public Object getItem(int position) {
			return taskList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = layoutInflater.inflate(R.layout.setting2_item, null);
			
			TaskItemView tview = new TaskItemView();
			AirplaneTaskObj tobj = taskList.get(position);
			
			tview.setDayView((TextView)convertView.findViewById(R.id.textView_item_title));
			tview.setCloseTimeView((TextView)convertView.findViewById(R.id.edtV_item_close_time));
			tview.setOpenTimeView((TextView)convertView.findViewById(R.id.edtV_item_startup_time));
			tview.setTurnrightCloseTimeBtn(tobj,(Button)convertView.findViewById(R.id.turnright_close_time));
			tview.setTurnrightOpenTimeBtn(tobj,(Button)convertView.findViewById(R.id.turnright_startup_time));
			
			
			tview.getDayView().setText(week[tobj.getDayId()-1]);
			tview.getCloseTimeView().setText(tobj.getTurnOffTime());
			tview.getOpenTimeView().setText(tobj.getTurnOnTime());
			
			return convertView;
		}
		
	}
	
	class TaskItemView{
		private TextView dayView;
		private TextView closeTimeView;
		private TextView openTimeView;
		private Button turnrightCloseTimeBtn;
		private Button turnrightOpenTimeBtn;
		public TextView getDayView() {
			return dayView;
		}
		public void setDayView(TextView dayView) {
			this.dayView = dayView;
		}
		public void setCloseTimeView(EditText closeTimeView) {
			this.closeTimeView = closeTimeView;
		}

		public void setOpenTimeView(EditText openTimeView) {
			this.openTimeView = openTimeView;
		}
		public TextView getCloseTimeView() {
			return closeTimeView;
		}
		public void setCloseTimeView(TextView closeTimeView) {
			this.closeTimeView = closeTimeView;
		}
		public TextView getOpenTimeView() {
			return openTimeView;
		}
		public void setOpenTimeView(TextView openTimeView) {
			this.openTimeView = openTimeView;
		}

		public Button getTurnrightCloseTimeBtn() {
			return turnrightCloseTimeBtn;
		}
		public void setTurnrightCloseTimeBtn(final AirplaneTaskObj tobj,Button turnrightCloseTimeBtn) {
			this.turnrightCloseTimeBtn = turnrightCloseTimeBtn;

			this.turnrightCloseTimeBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String []times = tobj.getTurnOffTime().split(":");
					
					AirplaneModeTimePickerDialog time = new AirplaneModeTimePickerDialog(AirplaneModeCycleSettings.this, new 
							AirplaneModeTimePickerDialog.OnTimeSetListener() {
						
						@Override
						public void onTimeSet(int hourOfDay, int minute) {
							tobj.setTurnOffTime(airplaneTool.genTimeString(hourOfDay, minute));
							taskAdapter.notifyDataSetChanged();
							tobj.setTurnOffExecuted(false);
							airplaneTool.setTaskObjStr(tobj);
							
						}
					}, Integer.parseInt(times[0]), Integer.parseInt(times[1]));
					time.show();
				}
			});
		
		}
		public Button getTurnrightOpenTimeBtn() {
			return turnrightOpenTimeBtn;
		}
		public void setTurnrightOpenTimeBtn(final AirplaneTaskObj tobj,Button turnrightOpenTimeBtn) {
			this.turnrightOpenTimeBtn = turnrightOpenTimeBtn;

			this.turnrightOpenTimeBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String []times = tobj.getTurnOnTime().split(":");

					AirplaneModeTimePickerDialog time = new AirplaneModeTimePickerDialog(AirplaneModeCycleSettings.this, new 
							AirplaneModeTimePickerDialog.OnTimeSetListener() {
						
						@Override
						public void onTimeSet(int hourOfDay, int minute) {
							tobj.setTurnOnTime(airplaneTool.genTimeString(hourOfDay, minute));
							taskAdapter.notifyDataSetChanged();
							tobj.setTurnOnExecuted(false);
							airplaneTool.setTaskObjStr(tobj);
							
						}
					}, Integer.parseInt(times[0]), Integer.parseInt(times[1]));
					time.show();
				}
			});
		
		}
		
	}
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		airplaneTool.tabHost.setCurrentTab(0);
    	}

    	return true;
   	
    }

}
